package com.kcwl.carrier.infrastructure.config;

import com.kcwl.carrier.infrastructure.repository.properties.MybatisPlusProperties;
import com.kcwl.framework.utils.StringUtil;
import com.kcwl.tenant.interceptor.TenantMybatisInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: wyl
 * @create: 2022/6/15 16:24
 * @version: 1.0
 * @desc: mybatisplus插件
 */
@Configuration
public class MyBatisPlusConfig {
    @Resource
    MybatisPlusProperties mybatisPlusProperties;

    //    @Bean
//    public PaginationInterceptor paginationInterceptor() {
//        return new PaginationInterceptor();
//    }
    @Bean
    public TenantMybatisInterceptor tenantMybatisInterceptor() {
        TenantMybatisInterceptor tenantMybatisInterceptor = new TenantMybatisInterceptor();
        tenantMybatisInterceptor.initSqlParser(mybatisPlusProperties.getPlatformNo(), getTenantTables(), mybatisPlusProperties.isEnabled(), new HashSet<>(mybatisPlusProperties.getFilterSql()), false, false);
        return tenantMybatisInterceptor;
    }

    private Set<String> getTenantTables() {
        if (StringUtil.isEmpty(mybatisPlusProperties.getTenantTables())) {
            return new HashSet<>();
        } else {
            String[] tenantTables = mybatisPlusProperties.getTenantTables().split(",");
            return new HashSet<>(Arrays.asList(tenantTables));
        }
    }
}
